﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class FrmaddTeacher : Form
    {
        public FrmaddTeacher()
        {
            InitializeComponent();
        }
        private string o_id = null;
        public FrmaddTeacher(string id)
        {
            InitializeComponent();
            this.o_id = id;
            if(o_id!="")
            {
                TeacherInfoData info = new TeacherInfoData();
                info.Teaid = id;
                DataSet ds = TeacherInfoOperation.getTeacherInfo(info);
                this.txtTeaid.Text = ds.Tables[0].Rows[0]["教师编号"].ToString();
                this.txtTeaname.Text = ds.Tables[0].Rows[0]["教师姓名"].ToString();
                this.txtTeaBirthday.Text = ds.Tables[0].Rows[0]["出生日期"].ToString();
                this.combTearSex.Text = ds.Tables[0].Rows[0]["教师性别"].ToString();
                this.txtTeloffice.Text = ds.Tables[0].Rows[0]["办公室电话"].ToString();
                this.txtAddress.Text = ds.Tables[0].Rows[0]["地址"].ToString();
                this.txtTeaid.Enabled = false;
                this.Text = "教师信息修改";

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string teaid = this.txtTeaid.Text;
            string teaname = this.txtTeaname.Text;
            string teaBirthday = this.txtTeaBirthday.Text;
            string tearSex = (string)this.combTearSex.Text;
            string teloffice = this.txtTeloffice.Text;
            string address = this.txtAddress.Text;
            if(teaid==null||teaid.Trim().Equals(""))
            {//教师代号不允许为空
                MessageBox.Show("前请输入教师代号！","提示");
                this.txtTeaid.Focus();
                return;
            }
            if(teaname==null||teaname.Trim().Equals(""))
            {//教师姓名不允许为空
                MessageBox.Show("请输入教师姓名！","提示");
                this.txtTeaname.Focus();
                return;
            }
            TeacherInfoData data = new TeacherInfoData();
            data.Teaid = teaid;
            data.Teaname = teaname;
            data.TeaBirthday = teaBirthday;
            data.TearSex = tearSex;
            data.Telooffice = teloffice;
            data.Address = address;
            try
            {
                if (o_id == "")
                {
                    if (TeacherInfoOperation.insertTeacherInfo(data))
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.txtTeaid.Text = "";
                        this.txtTeaname.Text = "";
                        this.txtTeaBirthday.Text = "";
                        this.txtTeloffice.Text = "";
                        this.txtAddress.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "错误");
                    }
                }
                else
                {
                    if (TeacherInfoOperation.updateTeacherInfo(data))
                    {
                        MessageBox.Show("修改成功！", "提示");
                        //((Frmstuxxcx)this.Parent).bindDataGird();
                        //this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！","错误");
                    }
                }
            }
            catch(Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败!","错误");
            }

        }

        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}